<template>
  <div>
    <!-- 版本 -->
    <BasicTable @register="registerTable" />
  </div>
</template>

<script setup lang="ts">
  import { onMounted, getCurrentInstance } from 'vue';
  import { BasicTable, useTable } from '/@/components/Table';
  import { GetPartRevisionTable } from '/@/api/demo/partManager';

  import { getVersionShowColumns } from '/@/views/demo/partManager/components/tableData';

  const { proxy }: any = getCurrentInstance();
  const props = defineProps({
    // 零件属性
    currentRecord: {
      type: Object,
      default: () => ({}),
    },
  });

  onMounted(() => {
    // 获取版本表格数据
    setTimeout(async () => {
      const res = await GetPartRevisionTable(props.currentRecord.oId, props.currentRecord.version);
      const keysToUpper = ['version', 'status'];

      res.listPartHeader.forEach((column) => {
        if (keysToUpper.includes(column.dataIndex)) {
          column.dataIndex = column.dataIndex.toUpperCase();
        }
        if (column.slots && keysToUpper.includes(column.slots.customRender)) {
          column.slots.customRender = column.slots.customRender.toUpperCase();
        }
      });

      setColumns(res.listPartHeader);
      const updatedData = res.partDt.map((item) => {
        const newItem = {};
        for (const key in item) {
          let value = item[key];

          // 转换字段名为首字母大写或驼峰对应
          const correctedKey =
            key === 'thitObjectId'
              ? 'ThitObjectId'
              : key === 'mark'
              ? 'Mark'
              : key === 'createTime'
              ? 'createtime'
              : key === 'effectTime'
              ? 'effecttime'
              : key === 'status'
              ? 'STATUS'
              : key === 'version'
              ? 'VERSION'
              : key;

          // 特殊字段处理：version 和 status 转换为字符串
          if (key === 'version' || key === 'status') {
            value = value != null ? String(value) : '';
          }
          // 处理状态过滤
          if (key === 'status') {
            value = value == null ? '' : proxy.$filters.filterPartStatus(value);
          }

          newItem[correctedKey] = value ?? '';
        }
        return newItem;
      });
      setTableData(updatedData);
    }, 500);
  });

  // 初始化表格
  const [registerTable, { setColumns, setTableData }] = useTable({
    columns: getVersionShowColumns(),
    bordered: true,
    showIndexColumn: false,
    showTableSetting: true, // 显示表格设置工具
    tableSetting: { fullScreen: true }, // 表格设置工具配置
    // pagination: { pageSize: 5, pageSizeOptions: ['5', '10', '15', '20'], defaultPageSize: 5 },
    canResize: true,
    scroll: { y: 200 },
  });
</script>
